package com.ruoyi.web.controller.compensationManagement;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.compensationManagement.domain.CompensationDeductionAmountSettings;
import com.ruoyi.compensationManagement.domain.vo.CompensationStaffPayrollVo;
import com.ruoyi.compensationManagement.domain.vo.SalaryFileVo;
import com.ruoyi.compensationManagement.domain.vo.StaffPayrollImportVo;
import com.ruoyi.compensationManagement.service.ICompensationDeductionAmountSettingsService;
import com.ruoyi.out.service.AttendanceManagementService;
import com.ruoyi.system.service.ISysUserPositionLevelHistoryService;
import com.ruoyi.system.service.ISysUserSalaryHistoryService;
import com.ruoyi.system.service.ISysUserService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.compensationManagement.domain.CompensationStaffpayroll;
import com.ruoyi.compensationManagement.service.ICompensationStaffpayrollService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工资表Controller
 *
 * @author ruoyi
 * @date 2022-10-09
 */
@RestController
@RequestMapping("/compensationManagement/staffPayroll")
public class CompensationStaffpayrollController extends BaseController {
    @Autowired
    private ICompensationStaffpayrollService compensationStaffpayrollService;
    @Autowired
    private AttendanceManagementService attendanceManagementService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ICompensationDeductionAmountSettingsService compensationDeductionAmountSettingsService;
    @Autowired
    private ISysUserPositionLevelHistoryService sysUserPositionLevelHistoryService;
    @Autowired
    private ISysUserSalaryHistoryService sysUserSalaryHistoryService;

    /**
     * 查询工资表列表
     */
    @PreAuthorize("@ss.hasPermi('compensationManagement:staffPayroll:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompensationStaffpayroll compensationStaffpayroll) {
        startPage();
        List<CompensationStaffpayroll> list = compensationStaffpayrollService.selectCompensationStaffpayrollList(compensationStaffpayroll);
        return getDataTable(list);
    }

    /**
     * 导出工资条列表
     */
    @PreAuthorize("@ss.hasPermi('compensationManagement:staffPayroll:exportArticleWages')")
    @Log(title = "工资条", businessType = BusinessType.EXPORT)
    @PostMapping("/exportArticleWages")
    public void exportArticleWages(HttpServletResponse response, CompensationStaffpayroll compensationStaffpayroll) {
        List<CompensationStaffPayrollVo> list = compensationStaffpayrollService.selectCompensationStaffpayrollListToConvert(compensationStaffpayroll);
        ExcelUtil<CompensationStaffPayrollVo> util = new ExcelUtil<CompensationStaffPayrollVo>(CompensationStaffPayrollVo.class);
        util.exportExcel(response, list, "工资条数据", "工资条数据");
    }

    /**
     * 导出工资档案
     */
    @PreAuthorize("@ss.hasPermi('compensationManagement:staffPayroll:exportSalaryFiles')")
    @Log(title = "工资档案", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSalaryFiles")
    public void exportSalaryFiles(HttpServletResponse response, CompensationStaffpayroll compensationStaffpayroll) {
        List<SalaryFileVo> list = compensationStaffpayrollService.selectCompensationStaffpayrollListToConvertSalaryFiles(compensationStaffpayroll);
        ExcelUtil<SalaryFileVo> util = new ExcelUtil<SalaryFileVo>(SalaryFileVo.class);
        util.exportExcel(response, list, "工资档案信息", "工资档案信息");
    }

    /**
     * 获取工资表详细信息
     */
    @PreAuthorize("@ss.hasPermi('compensationManagement:staffPayroll:query')")
    @GetMapping(value = "/{staffpayrollId}")
    public AjaxResult getInfo(@PathVariable("staffpayrollId") Long staffpayrollId) {
        return AjaxResult.success(compensationStaffpayrollService.selectCompensationStaffpayrollByStaffpayrollId(staffpayrollId));
    }

    /**
     * 新增工资表
     */
    @PreAuthorize("@ss.hasPermi('compensationManagement:staffPayroll:add')")
    @Log(title = "工资表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompensationStaffpayroll compensationStaffpayroll) {
        compensationStaffpayroll.setDeductionsPart(JSON.toJSONString(compensationStaffpayroll.getDeductionsPartSql()));
        return toAjax(compensationStaffpayrollService.insertCompensationStaffpayroll(compensationStaffpayroll));
    }

    /**
     * 修改工资表
     */
    @PreAuthorize("@ss.hasPermi('compensationManagement:staffPayroll:edit')")
    @Log(title = "工资表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompensationStaffpayroll compensationStaffpayroll) {
        return toAjax(compensationStaffpayrollService.updateCompensationStaffpayroll(compensationStaffpayroll));
    }

    /**
     * 删除工资表
     */
    @PreAuthorize("@ss.hasPermi('compensationManagement:staffPayroll:remove')")
    @Log(title = "工资表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{staffpayrollIds}")
    public AjaxResult remove(@PathVariable Long[] staffpayrollIds) {
        return toAjax(compensationStaffpayrollService.deleteCompensationStaffpayrollByStaffpayrollIds(staffpayrollIds));
    }

    /**
     * 根据工号工资表信息
     */
    @PreAuthorize("@ss.hasPermi('compensationManagement:staffPayroll:getPayrollInformation')")
    @PostMapping("/getPayrollInformationByJobNumber")
    public AjaxResult getPayrollInformationByJobNumber(@RequestBody Map<String, Object> map) {
        Map<String, Object> stringObjectMap = new HashMap<>();
        SysUser user = sysUserService.getSysUserByJobNumber(map.get("jobnumber").toString());
        if (StringUtils.isNull(user)) {
            return AjaxResult.error("工号错误，没有该工号的员工信息");
        }
        stringObjectMap.put("userId", user.getUserId());
        stringObjectMap.put("searchTime", map.get("date"));
        if (StringUtils.isEmpty(attendanceManagementService.list(stringObjectMap))) {
            return AjaxResult.error("该员工在" + map.get("date") + "月份时没有考勤数据");
        }
        if (StringUtils.isNull(sysUserPositionLevelHistoryService.getPositionsByUserIdAndNowDate(user.getUserId(), DateUtils.parseDate(map.get("date"))))) {
            return AjaxResult.error("该员工在" + map.get("date") + "月份时没有历史岗位信息");
        }
        if (StringUtils.isNull(sysUserSalaryHistoryService.getSalaryHistoryByUserIdAndNowDate(user.getUserId(), DateUtils.parseDate(map.get("date"))))){
            return AjaxResult.error("该员工在" + map.get("date") + "月份时没有历史薪级信息");
        }
        return AjaxResult.success(compensationStaffpayrollService.getPayrollInformationByJobNumber(map.get("jobnumber").toString(), DateUtils.parseDate(map.get("date"))));
    }

    /**
     * 确定工资表
     */
    @PreAuthorize("@ss.hasPermi('compensationManagement:staffPayroll:determiningStaffPayroll')")
    @PutMapping("determiningStaffPayroll")
    public AjaxResult determiningStaffPayroll(@RequestBody CompensationStaffpayroll compensationStaffpayroll) {
        return AjaxResult.success(compensationStaffpayrollService.determiningStaffPayroll(compensationStaffpayroll));
    }

    @Log(title = "工资导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('compensationManagement:staffPayroll:importData')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<StaffPayrollImportVo> util = new ExcelUtil<StaffPayrollImportVo>(StaffPayrollImportVo.class);
        List<StaffPayrollImportVo> staffPayrollList = util.importExcel(file.getInputStream());
        String message = compensationStaffpayrollService.importStaffPayroll(staffPayrollList);
        return AjaxResult.success(message);
    }


    /**
     * 导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<StaffPayrollImportVo> util = new ExcelUtil<StaffPayrollImportVo>(StaffPayrollImportVo.class);
        util.importTemplateExcel(response, "工资导入");
    }

    /**
     * 新增时查询浮动项目
     *
     * @return
     */
    @GetMapping("/floatSelectItem")
    public AjaxResult floatSelectItem() {
        return AjaxResult.success(compensationDeductionAmountSettingsService.selectCompensationDeductionAmountSettingsList(new CompensationDeductionAmountSettings()));
    }
}
